#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QDateTime>
#include <QDir>
OrignalDataUi originalDataUi;

QMutex Locker;

QString saveFileName;
int saveDataCount=0;
extern DockWidgets dockWidgets;
//关掉widget的时候 wdigetPtr置空，isInUse置false.新建widget的时候，从头开始遍历的第一个isInUSe=false的窗口。
//发送信号的时候发送id和QByteArray只有与之id相同的才会解析
//连接信号与槽的时候 连接该对象的信号与该对象的槽
//如何轮询
//修改widget数目的时候 从0开始轮询
//修改widget数目的时候要停止通讯

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    CreateMenueBars();
    GetAvailablePorts();
    ReadPortDefaultValue();
    RadioButtonChanged();
    SaveDefaultPorts(true);
    SlotsInit();
    ReadDebugConfig();
    SetStyleSheet();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::GetAvailablePorts()
{
    for(QSerialPortInfo x:QSerialPortInfo::availablePorts())
    {
        ui->comboBox->addItem(x.portName());
        ui->comboBox_3->addItem(x.portName());
        ui->comboBox_11->addItem(x.portName());
        ui->comboBox_16->addItem(x.portName());
    }
}

void MainWindow::ReadPortDefaultValue()
{

    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);

    QFile file;
    file.setFileName("串口配置.ini");

    if(file.open(QFile::ReadOnly))
    {
        while (!file.atEnd())
        {
            QByteArray read=file.readLine();
            QString readStr=read.toStdString().c_str();
            QString tmpStr=readStr;
            if(tmpStr.contains(u8"串口1")||tmpStr.contains(u8"串口2")||tmpStr.contains(u8"串口3")||tmpStr.contains(u8"串口4"))
            {
                QComboBox * combo[5]={0};
                if(tmpStr.contains(u8"串口1"))
                {
                    combo[0]=ui->comboBox;
                    combo[1]=ui->comboBox_2;
                    combo[2]=ui->comboBox_4;
                    combo[3]=ui->comboBox_5;
                    combo[4]=ui->comboBox_6;
                }
                else if(tmpStr.contains(u8"串口2"))
                {
                    combo[0]=ui->comboBox_3;
                    combo[1]=ui->comboBox_7;
                    combo[2]=ui->comboBox_8;
                    combo[3]=ui->comboBox_9;
                    combo[4]=ui->comboBox_10;
                }
                else if(tmpStr.contains(u8"串口3"))
                {
                    combo[0]=ui->comboBox_11;
                    combo[1]=ui->comboBox_12;
                    combo[2]=ui->comboBox_13;
                    combo[3]=ui->comboBox_14;
                    combo[4]=ui->comboBox_15;
                }
                else if(tmpStr.contains(u8"串口4"))
                {
                    combo[0]=ui->comboBox_16;
                    combo[1]=ui->comboBox_17;
                    combo[2]=ui->comboBox_18;
                    combo[3]=ui->comboBox_19;
                    combo[4]=ui->comboBox_20;
                }
                //端口号
                QString portName;
                portName=tmpStr.remove(0,tmpStr.indexOf("=")+1);                                    //除去前面的 串口=
                portName=tmpStr.remove(tmpStr.indexOf(","),tmpStr.length()-tmpStr.indexOf(","));    //除去后面的,,9600bps,NONE,8,1
                if(combo[0]->findText(portName)!=-1)
                {
                    combo[0]->setCurrentText(portName);
                }
                qDebug()<<"读配置文件解析出来的端口号为:"<<__LINE__<<tmpStr;

                //ui->radioButton->setText(combo[0]->currentText())
                //波特率
                tmpStr=readStr;
                QString baurdRate=tmpStr.remove(0,tmpStr.indexOf(',')+1);         //除去前面的串口1=COM1,
                baurdRate=baurdRate.remove(baurdRate.indexOf(","),baurdRate.length()-baurdRate.indexOf(','));
                baurdRate.remove("bps");
                if(combo[1]->findText(baurdRate)!=-1)
                {
                    combo[1]->setCurrentText(baurdRate);
                }
                else
                    combo[1]->setCurrentIndex(0);
                qDebug()<<"读配置文件解析出来的波特率为:"<<baurdRate;

                //校验位
                tmpStr=readStr;
                QString parityBit=tmpStr.remove(0,tmpStr.indexOf(',')+1);                                           //除去前面的串口=com1,
                parityBit=parityBit.remove(0,parityBit.indexOf(',')+1);                                             //除去前面的9600,
                parityBit=parityBit.remove(parityBit.indexOf(","),parityBit.length()-parityBit.indexOf(','));       //除去后面的,8,1

                qDebug()<<"读配置文件解析出来的校验位为:"<<parityBit;

                if(combo[2]->findText(parityBit)!=-1)
                {
                    combo[2]->setCurrentText(parityBit);
                }
                else
                {
                    combo[2]->setCurrentIndex(0);
                }
                //数据位
                tmpStr=readStr;
                QString dataBit=tmpStr.remove(0,tmpStr.indexOf(',')+1);                                         //除去前面的串口=com1,
                dataBit=dataBit.remove(0,dataBit.indexOf(',')+1);                                               //除去前面的9600,
                dataBit=dataBit.remove(0,dataBit.indexOf(',')+1);                                               //除去前面的NONE,
                dataBit=dataBit.remove(dataBit.indexOf(","),dataBit.length()-dataBit.indexOf(','));             //除去后面的,1

                qDebug()<<"读配置文件解析出来的数据位为:"<<dataBit;

                if(combo[3]->findText(parityBit)!=-1)
                {
                    combo[3]->setCurrentText(parityBit);
                }
                else
                {
                    combo[3]->setCurrentIndex(0);
                }
                //停止位
                tmpStr=readStr;
                QString stopBit=tmpStr.remove(0,tmpStr.indexOf(',')+1);                                       //除去前面的串口=com1,
                stopBit=stopBit.remove(0,stopBit.indexOf(',')+1);                                             //除去前面的9600,
                stopBit=stopBit.remove(0,stopBit.indexOf(',')+1);                                             //除去前面的NONE,
                stopBit=stopBit.remove(0,stopBit.indexOf(',')+1);                                             //除去前面的8,
                stopBit.remove("\r\n");

                qDebug()<<"读配置文件解析出来的停止位为:"<<stopBit;

                if(combo[4]->findText(parityBit)!=-1)
                {
                    combo[4]->setCurrentText(parityBit);
                }
                else
                {
                    combo[4]->setCurrentIndex(0);
                }

                QString usePortName;
                int useBaurdRate;
                QSerialPort::DataBits dataBits;
                QSerialPort::StopBits stopBits;
                QSerialPort::Parity parity;

                usePortName=portName;
                useBaurdRate=baurdRate.toUInt();
                dataBits=(QSerialPort::DataBits)dataBit.toInt();
                stopBits=(QSerialPort::StopBits)stopBit.toInt();

                parity=QSerialPort::NoParity;
                if(parityBit=="NONE")
                {
                    parity=QSerialPort::NoParity;
                }
                else if (parityBit=="ODD")
                {
                    parity=QSerialPort::OddParity;
                }
                else if(parityBit=="EVEN")
                {
                    parity=QSerialPort::EvenParity;
                }

                if(combo[0]==ui->comboBox)
                {
                    ui->radioButton->setText(ui->comboBox->currentText());
                    device1.modbus->SetPortInfo(combo[0]->currentText(),useBaurdRate,dataBits,stopBits,parity);
                }

                if(combo[0]==ui->comboBox_3)
                {
                    ui->radioButton_2->setText(ui->comboBox_3->currentText());
                    device2.modbus->SetPortInfo(combo[0]->currentText(),useBaurdRate,dataBits,stopBits,parity);
                }

                if(combo[0]==ui->comboBox_11)
                {
                    ui->radioButton_5->setText(ui->comboBox_11->currentText());
                    device3.modbus->SetPortInfo(combo[0]->currentText(),useBaurdRate,dataBits,stopBits,parity);
                }

                if(combo[0]==ui->comboBox_16)
                {
                    ui->radioButton_6->setText(ui->comboBox_16->currentText());
                    device4.modbus->SetPortInfo(combo[0]->currentText(),useBaurdRate,dataBits,stopBits,parity);
                }
            }
        }
    }

}

void MainWindow::SlotsInit()
{
    connect(ui->pushButton,SIGNAL(clicked(bool)),this,SLOT(SaveDefaultPorts(bool)));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(LoadSavePorts()));
    connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(TabWidgetChanged(int)));
    connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(StartCommunication()));
    connect(ui->pushButton_4,SIGNAL(clicked()),this,SLOT(StopCommunication()));

    connect(ui->radioButton,SIGNAL(clicked()),this,SLOT(RadioButtonChanged()));
    connect(ui->radioButton_2,SIGNAL(clicked()),this,SLOT(RadioButtonChanged()));
    connect(ui->radioButton_5,SIGNAL(clicked()),this,SLOT(RadioButtonChanged()));
    connect(ui->radioButton_6,SIGNAL(clicked()),this,SLOT(RadioButtonChanged()));

    connect(action2,SIGNAL(triggered()),this,SLOT(NewFc3DockWidget()));
    connect(action3,SIGNAL(triggered()),this,SLOT(NewFc4DockWidget()));


    qRegisterMetaType<RecvBuf>("RecvBuf");

    //调试界面的按钮
    connect(ui->pushButton_101,SIGNAL(clicked()),this,SLOT(Send1()));
    connect(ui->pushButton_102,SIGNAL(clicked()),this,SLOT(Send2()));
    connect(ui->pushButton_103,SIGNAL(clicked()),this,SLOT(Send3()));
    connect(ui->pushButton_104,SIGNAL(clicked()),this,SLOT(Send4()));
    connect(ui->pushButton_105,SIGNAL(clicked()),this,SLOT(Send5()));
    connect(ui->pushButton_106,SIGNAL(clicked()),this,SLOT(Send6()));
    connect(ui->pushButton_107,SIGNAL(clicked()),this,SLOT(Send7()));
    connect(ui->pushButton_108,SIGNAL(clicked()),this,SLOT(Send8()));
    connect(ui->pushButton_109,SIGNAL(clicked()),this,SLOT(Send9()));
    connect(ui->pushButton_110,SIGNAL(clicked()),this,SLOT(Send10()));
    connect(ui->pushButton_111,SIGNAL(clicked()),this,SLOT(Send11()));
    connect(ui->pushButton_112,SIGNAL(clicked()),this,SLOT(Send12()));
    connect(ui->pushButton_113,SIGNAL(clicked()),this,SLOT(Send13()));
    connect(ui->pushButton_114,SIGNAL(clicked()),this,SLOT(Send14()));
    connect(ui->pushButton_115,SIGNAL(clicked()),this,SLOT(Send15()));
    connect(ui->pushButton_116,SIGNAL(clicked()),this,SLOT(Send16()));
    connect(ui->pushButton_117,SIGNAL(clicked()),this,SLOT(Send17()));
    connect(ui->pushButton_118,SIGNAL(clicked()),this,SLOT(Send18()));
    connect(ui->pushButton_119,SIGNAL(clicked()),this,SLOT(Send19()));
    connect(ui->pushButton_120,SIGNAL(clicked()),this,SLOT(Send20()));
    connect(ui->pushButton_121,SIGNAL(clicked()),this,SLOT(Send21()));
    connect(ui->pushButton_122,SIGNAL(clicked()),this,SLOT(Send22()));
    connect(ui->pushButton_123,SIGNAL(clicked()),this,SLOT(Send23()));
    connect(ui->pushButton_124,SIGNAL(clicked()),this,SLOT(Send24()));
    connect(ui->pushButton_125,SIGNAL(clicked()),this,SLOT(Send25()));
    connect(ui->pushButton_126,SIGNAL(clicked()),this,SLOT(Send26()));
    connect(ui->pushButton_127,SIGNAL(clicked()),this,SLOT(Send27()));
    connect(ui->pushButton_128,SIGNAL(clicked()),this,SLOT(Send28()));
    connect(ui->pushButton_129,SIGNAL(clicked()),this,SLOT(Send29()));
    connect(ui->pushButton_130,SIGNAL(clicked()),this,SLOT(Send30()));
    connect(ui->pushButton_131,SIGNAL(clicked()),this,SLOT(Send31()));
    connect(ui->pushButton_132,SIGNAL(clicked()),this,SLOT(Send32()));

    connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(OpenPort()));
    connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(OpenPort2()));
    connect(ui->pushButton_9,SIGNAL(clicked()),this,SLOT(OpenPort3()));
    connect(ui->pushButton_10,SIGNAL(clicked()),this,SLOT(OpenPort4()));

    connect(&device1,SIGNAL(To_ShowRecvData(QByteArray)),this,SLOT(ShowRecvData(QByteArray)));
    connect(&device2,SIGNAL(To_ShowRecvData(QByteArray)),this,SLOT(ShowRecvData(QByteArray)));
    connect(&device3,SIGNAL(To_ShowRecvData(QByteArray)),this,SLOT(ShowRecvData(QByteArray)));
    connect(&device4,SIGNAL(To_ShowRecvData(QByteArray)),this,SLOT(ShowRecvData(QByteArray)));

}

void MainWindow::SetStyleSheet()
{
    QFile f;
    f.setFileName(":/qss/styleSheet.qss");
    if(f.open(QFile::ReadOnly))
    {
        QByteArray read=f.readAll();
        this->setStyleSheet(read.toStdString().c_str());
        f.close();
    }
    else
    {
        qDebug()<<"样式表:打开失败";

    }

}

void MainWindow::SetRadioButtonUseable(bool value)
{
    ui->radioButton->setEnabled(value);
    ui->radioButton_2->setEnabled(value);
    ui->radioButton_5->setEnabled(value);
    ui->radioButton_6->setEnabled(value);
    ui->spinBox->setEnabled(value);
}

void MainWindow::StartCommunication()
{
    qDebug()<<__FUNCTION__;
    SetRadioButtonUseable(false);
    device->deviceAddress=ui->spinBox->value();
    device->StopTimer();
    device->OpenDevice();
    device->StartTimer();
}

void MainWindow::StopCommunication()
{
    qDebug()<<__FUNCTION__;
    SetRadioButtonUseable(true);
    device->StopTimer();
}

void MainWindow::SaveDefaultPorts(bool value)
{
    //关闭正在进行的界面3的读取操作
    StopRead();
    QThread::msleep(150);

    qDebug()<<value;
    /**********************默认值*************************/
    QString usePortName="COM1";
    int useBaurdRate=9600;
    QSerialPort::DataBits dataBits=QSerialPort::Data8;
    QSerialPort::StopBits stopBits=QSerialPort::OneStop;
    QSerialPort::Parity parity=QSerialPort::NoParity;

    //将port1的信息存入PortConfig.ini
    /**********************port1*************************/
    QSettings set("PortConfig.ini",QSettings::IniFormat);
    set.beginGroup("port");
    set.setValue("port1",ui->comboBox->currentText());
    set.setValue("baurdRate1",ui->comboBox_2->currentText());
    set.setValue("parity1",ui->comboBox_4->currentText());
    set.setValue("dataBit1",ui->comboBox_5->currentText());
    set.setValue("stopBit1",ui->comboBox_6->currentText()) ;

    usePortName=ui->comboBox->currentText();
    useBaurdRate=ui->comboBox_2->currentText().toInt();
    if(ui->comboBox_4->currentText()=="NONE")
    {
        parity=QSerialPort::NoParity;
    }
    else if (ui->comboBox_4->currentText()=="ODD")
    {
        parity=QSerialPort::OddParity;
    }
    else if(ui->comboBox_4->currentText()=="EVEN")
    {
        parity=QSerialPort::EvenParity;
    }
    else
        parity=QSerialPort::NoParity;
    dataBits=(QSerialPort::DataBits)ui->comboBox_5->currentText().toInt();
    stopBits=(QSerialPort::StopBits)ui->comboBox_6->currentText().toInt();

    //qDebug()<<dataBits<<stopBits;

    device1.modbus->SetPortInfo(usePortName,useBaurdRate,dataBits,stopBits,parity);



    /**********************port2*************************/
    set.setValue("port2",ui->comboBox_3->currentText());
    set.setValue("baurdRate2",ui->comboBox_7->currentText());
    set.setValue("parity2",ui->comboBox_8->currentText());
    set.setValue("dataBit2",ui->comboBox_9->currentText());
    set.setValue("stopBit2",ui->comboBox_10->currentText()) ;

    usePortName=ui->comboBox_3->currentText();
    useBaurdRate=ui->comboBox_7->currentText().toInt();
    if(ui->comboBox_8->currentText()=="NONE")
    {
        parity=QSerialPort::NoParity;
    }
    else if (ui->comboBox_8->currentText()=="ODD")
    {
        parity=QSerialPort::OddParity;
    }
    else if(ui->comboBox_8->currentText()=="EVEN")
    {
        parity=QSerialPort::EvenParity;
    }
    else
        parity=QSerialPort::NoParity;
    dataBits=(QSerialPort::DataBits)ui->comboBox_9->currentText().toInt();
    stopBits=(QSerialPort::StopBits)ui->comboBox_10->currentText().toInt();

    device2.modbus->SetPortInfo(usePortName,useBaurdRate,dataBits,stopBits,parity);

     /**********************port3*************************/
    set.setValue("port3",ui->comboBox_11->currentText());
    set.setValue("baurdRate3",ui->comboBox_12->currentText());
    set.setValue("parity3",ui->comboBox_13->currentText());
    set.setValue("dataBit3",ui->comboBox_14->currentText());
    set.setValue("stopBit3",ui->comboBox_15->currentText()) ;

    usePortName=ui->comboBox_11->currentText();
    useBaurdRate=ui->comboBox_12->currentText().toInt();
    if(ui->comboBox_13->currentText()=="NONE")
    {
        parity=QSerialPort::NoParity;
    }
    else if (ui->comboBox_13->currentText()=="ODD")
    {
        parity=QSerialPort::OddParity;
    }
    else if(ui->comboBox_13->currentText()=="EVEN")
    {
        parity=QSerialPort::EvenParity;
    }
    else
        parity=QSerialPort::NoParity;
    dataBits=(QSerialPort::DataBits)ui->comboBox_14->currentText().toInt();
    stopBits=(QSerialPort::StopBits)ui->comboBox_15->currentText().toInt();

    device3.modbus->SetPortInfo(usePortName,useBaurdRate,dataBits,stopBits,parity);

    /**********************port4*************************/
    set.setValue("port4",ui->comboBox_16->currentText());
    set.setValue("baurdRate4",ui->comboBox_17->currentText());
    set.setValue("parity4",ui->comboBox_18->currentText());
    set.setValue("dataBit4",ui->comboBox_19->currentText());
    set.setValue("stopBit4",ui->comboBox_20->currentText()) ;
    set.endGroup();

    usePortName=ui->comboBox_16->currentText();
    useBaurdRate=ui->comboBox_17->currentText().toInt();
    if(ui->comboBox_18->currentText()=="NONE")
    {
        parity=QSerialPort::NoParity;
    }
    else if (ui->comboBox_18->currentText()=="ODD")
    {
        parity=QSerialPort::OddParity;
    }
    else if(ui->comboBox_18->currentText()=="EVEN")
    {
        parity=QSerialPort::EvenParity;
    }
    dataBits=(QSerialPort::DataBits)ui->comboBox_19->currentText().toInt();
    stopBits=(QSerialPort::StopBits)ui->comboBox_20->currentText().toInt();

    device4.modbus->SetPortInfo(usePortName,useBaurdRate,dataBits,stopBits,parity);

    /*将串口号同步到读原始数据界面*/
    ui->radioButton->setText(ui->comboBox->currentText());
    ui->radioButton_2->setText(ui->comboBox_3->currentText());
    ui->radioButton_5->setText(ui->comboBox_11->currentText());
    ui->radioButton_6->setText(ui->comboBox_16->currentText());

    //设置通信间隔
    device1.timeout=ui->spinBox_2->value();
    device2.timeout=ui->spinBox_3->value();
    device3.timeout=ui->spinBox_4->value();
    device4.timeout=ui->spinBox_5->value();

    if(!value)
    {
        QMessageBox a;
        a.setText(u8"保存成功");
        a.show();
        a.exec();
    }

    StartRead();
}

void MainWindow::LoadSavePorts()
{
    qDebug()<<__FUNCTION__;
    QSettings set("PortConfig.ini",QSettings::IniFormat);
    set.beginGroup("port");
    ui->comboBox->setCurrentText(set.value("port1").toString());
    ui->comboBox_2->setCurrentText(set.value("baurdRate1").toString());
    ui->comboBox_4->setCurrentText(set.value("parity1").toString());
    ui->comboBox_5->setCurrentText(set.value("dataBit1").toString());
    ui->comboBox_6->setCurrentText(set.value("stopBit1").toString());

    ui->comboBox_3->setCurrentText(set.value("port2").toString());
    ui->comboBox_7->setCurrentText(set.value("baurdRate2").toString());
    ui->comboBox_8->setCurrentText(set.value("parity2").toString());
    ui->comboBox_9->setCurrentText(set.value("dataBit2").toString());
    ui->comboBox_10->setCurrentText(set.value("stopBit2").toString());

    ui->comboBox_11->setCurrentText(set.value("port3").toString());
    ui->comboBox_12->setCurrentText(set.value("baurdRate3").toString());
    ui->comboBox_13->setCurrentText(set.value("parity3").toString());
    ui->comboBox_14->setCurrentText(set.value("dataBit3").toString());
    ui->comboBox_15->setCurrentText(set.value("stopBit3").toString());

    ui->comboBox_16->setCurrentText(set.value("port4").toString());
    ui->comboBox_17->setCurrentText(set.value("baurdRate4").toString());
    ui->comboBox_18->setCurrentText(set.value("parity4").toString());
    ui->comboBox_19->setCurrentText(set.value("dataBit4").toString());
    ui->comboBox_20->setCurrentText(set.value("stopBit4").toString());
    set.endGroup();
}

void MainWindow::TabWidgetChanged(int index)
{
    qDebug()<<"1:"<<index;
    if(index==1)
    {
        device->StopTimer();
    }
}

void MainWindow::RadioButtonChanged()
{
    if(ui->radioButton->isChecked())
    {
        qDebug()<<"device1";
        device=&device1;
    }
    else if(ui->radioButton_2->isChecked())
    {
        qDebug()<<"device2";
        device=&device2;
    }
    else if(ui->radioButton_5->isChecked())
    {
        qDebug()<<"device3";
        device=&device3;
    }
    else if(ui->radioButton_6->isChecked())
    {
        qDebug()<<"device4";
        device=&device4;
    }
}

void MainWindow::NewFc3DockWidget()
{
    qDebug()<<__FILE__<<__LINE__<<__FUNCTION__;
    for(int i=0;i<=127;i++)
    {
        if(dockWidgets.element[i].isInUse==false)
        {
            //如果以前new过就不用重新new直接加到vector里使用
            if(dockWidgets.element[i].widgetPtr!=nullptr)
            {
                for(int j=0;j<=dockWidgets.element[i].widgetPtr->registerSum-1;j++)
                    dockWidgets.element[i].widgetPtr->dataUI.row[j].overflowTimes=0;

                dockWidgets.element[i].widgetPtr->registerStart=0;
                dockWidgets.element[i].widgetPtr->registerSum=0;
                dockWidgets.element[i].widgetPtr->sendFrameCount=0;
                dockWidgets.element[i].widgetPtr->recvFrameCount=0;


                dockWidgets.element[i].widgetPtr->UIInit();

                dockWidgets.element[i].widgetPtr->show();
                Locker.lock();
                dockWidgets.currentIDVector.append(i);
                dockWidgets.element[i].widgetPtr->currentID=i;
                dockWidgets.element[i].widgetPtr->type=1;
                dockWidgets.element[i].isInUse=true;
                //dockWidgets.element[i].widgetPtr->setAttribute(Qt::WA_DeleteOnClose);
                Locker.unlock();
                break;
            }
            else
            {
                DockWidget2 *dockWidget=nullptr;
                dockWidget=new DockWidget2(nullptr);
                dockWidget->show();
                Locker.lock();
                dockWidgets.element[i].widgetPtr=dockWidget;
                dockWidgets.currentIDVector.append(i);
                dockWidgets.element[i].widgetPtr->currentID=i;
                dockWidgets.element[i].widgetPtr->type=1;
                dockWidgets.element[i].isInUse=true;
                //dockWidgets.element[i].widgetPtr->setAttribute(Qt::WA_DeleteOnClose);
                Locker.unlock();
                qDebug()<<"新增:"<<i<<"type:"<<dockWidgets.element[i].widgetPtr->type;

                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device1,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device2,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device3,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device4,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);

                break;
            }

        }
    }
}

void MainWindow::NewFc4DockWidget()
{
    qDebug()<<__FILE__<<__LINE__<<__FUNCTION__;

    for(int i=0;i<=127;i++)
    {
        if(dockWidgets.element[i].isInUse==false)
        {
            if(dockWidgets.element[i].widgetPtr!=nullptr)
            {
                for(int j=0;j<=dockWidgets.element[i].widgetPtr->registerSum-1;j++)
                    dockWidgets.element[i].widgetPtr->dataUI.row[j].overflowTimes=0;

                dockWidgets.element[i].widgetPtr->registerStart=0;
                dockWidgets.element[i].widgetPtr->registerSum=0;
                dockWidgets.element[i].widgetPtr->sendFrameCount=0;
                dockWidgets.element[i].widgetPtr->recvFrameCount=0;



                dockWidgets.element[i].widgetPtr->UIInit();
                dockWidgets.element[i].widgetPtr->show();
                Locker.lock();
                dockWidgets.currentIDVector.append(i);
                dockWidgets.element[i].widgetPtr->currentID=i;
                dockWidgets.element[i].widgetPtr->type=2;
                dockWidgets.element[i].isInUse=true;
                //dockWidgets.element[i].widgetPtr->setAttribute(Qt::WA_DeleteOnClose);
                Locker.unlock();
                break;
            }
            else
            {
                DockWidget2 *dockWidget=nullptr;
                dockWidget=new DockWidget2(nullptr);
                dockWidget->show();
                Locker.lock();
                dockWidgets.element[i].widgetPtr=dockWidget;
                dockWidgets.currentIDVector.append(i);
                dockWidgets.element[i].widgetPtr->currentID=i;
                dockWidgets.element[i].widgetPtr->type=2;
                dockWidgets.element[i].isInUse=true;
                //dockWidgets.element[i].widgetPtr->setAttribute(Qt::WA_DeleteOnClose);
                Locker.unlock();
                qDebug()<<"新增:"<<i<<"type:"<<dockWidgets.element[i].widgetPtr->type;
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device1,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device2,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device3,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);
                connect(dockWidget,SIGNAL(CloseCommunitcation()),&device4,SLOT(Slots_StopTimer()),Qt::BlockingQueuedConnection);

                break;
            }
        }
    }
}

void MainWindow::Send1()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(0,ui->spinBox_101->value());
    HandleSyncButton(0);
    StartRead();
}

void MainWindow::Send2()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(1,ui->spinBox_102->value());
    HandleSyncButton(1);
    StartRead();
}

void MainWindow::Send3()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(2,ui->spinBox_103->value());
    HandleSyncButton(2);
    StartRead();
}

void MainWindow::Send4()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(3,ui->spinBox_104->value());
    HandleSyncButton(3);
    StartRead();
}

void MainWindow::Send5()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(4,ui->spinBox_105->value());
    HandleSyncButton(4);
    StartRead();
}

void MainWindow::Send6()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(5,ui->spinBox_106->value());
    HandleSyncButton(5);
    StartRead();
}

void MainWindow::Send7()
{
    StopRead();
    HandleButton(6,ui->spinBox_107->value());
    HandleSyncButton(6);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send8()
{
    StopRead();
    HandleButton(7,ui->spinBox_108->value());
    HandleSyncButton(7);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send9()
{
    StopRead();
    HandleButton(8,ui->spinBox_109->value());
    HandleSyncButton(8);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send10()
{
    StopRead();
    HandleButton(9,ui->spinBox_110->value());
    HandleSyncButton(9);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send11()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(10,ui->spinBox_111->value());
    HandleSyncButton(10);
    StartRead();
}

void MainWindow::Send12()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(11,ui->spinBox_112->value());
    HandleSyncButton(11);
    StartRead();
}

void MainWindow::Send13()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(12,ui->spinBox_113->value());
    HandleSyncButton(12);
    StartRead();
}

void MainWindow::Send14()
{
    StopRead();
    HandleButton(13,ui->spinBox_114->value());
    HandleSyncButton(13);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send15()
{
    StopRead();
    HandleButton(14,ui->spinBox_115->value());
    HandleSyncButton(14);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send16()
{
    StopRead();
    HandleButton(15,ui->spinBox_116->value());
    HandleSyncButton(15);
    StartRead();
    qDebug()<<__FUNCTION__<<__LINE__;

}

void MainWindow::Send17()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(16,ui->spinBox_117->value());
    HandleSyncButton(16);
    StartRead();
}

void MainWindow::Send18()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(17,ui->spinBox_118->value());
    HandleSyncButton(17);
    StartRead();
}

void MainWindow::Send19()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(18,ui->spinBox_119->value());
    HandleSyncButton(18);
    StartRead();
}

void MainWindow::Send20()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(19,ui->spinBox_120->value());
    HandleSyncButton(19);
    StartRead();
}

void MainWindow::Send21()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(20,ui->spinBox_121->value());
    HandleSyncButton(20);
    StartRead();
}

void MainWindow::Send22()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(21,ui->spinBox_122->value());
    HandleSyncButton(21);
    StartRead();
}

void MainWindow::Send23()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(22,ui->spinBox_123->value());
    HandleSyncButton(22);
    StartRead();
}

void MainWindow::Send24()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(23,ui->spinBox_124->value());
    HandleSyncButton(23);
    StartRead();
}

void MainWindow::Send25()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(24,ui->spinBox_125->value());
    HandleSyncButton(24);
    StartRead();
}

void MainWindow::Send26()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(25,ui->spinBox_126->value());
    HandleSyncButton(25);
    StartRead();
}

void MainWindow::Send27()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(26,ui->spinBox_127->value());
    HandleSyncButton(26);
    StartRead();
}

void MainWindow::Send28()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(27,ui->spinBox_128->value());
    HandleSyncButton(27);
    StartRead();
}

void MainWindow::Send29()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(28,ui->spinBox_129->value());
    HandleSyncButton(28);
    StartRead();
}

void MainWindow::Send30()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(29,ui->spinBox_130->value());
    HandleSyncButton(29);
    StartRead();
}

void MainWindow::Send31()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(30,ui->spinBox_131->value());
    HandleSyncButton(30);
    StartRead();
}

void MainWindow::Send32()
{
    qDebug()<<__FUNCTION__<<__LINE__;
    StopRead();
    HandleButton(31,ui->spinBox_132->value());
    HandleSyncButton(31);
    StartRead();
}

void MainWindow::CreateMenueBars()
{
    QMenuBar *menuBar = this->menuBar();

    // 创建顶级菜单
    QMenu *fileMenu = menuBar->addMenu("新建");

    action2=new QAction("读参数(0x03)",fileMenu);
    action3=new QAction("读原始数据(0x04)",fileMenu);

    fileMenu->addAction(action2);
    fileMenu->addAction(action3);
}
